<?php


class AreaImageTable extends Doctrine_Table
{
  public static function getInstance()
  {
    return Doctrine_Core::getTable('AreaImage');
  }
  
  public function getBackendListQuery(Doctrine_Query $query)
  {
    $rootAlias = $query->getRootAlias();
    
    $user = sfContext::getInstance()->getUser();
    
    if (!$user->hasCredential('super'))
    {
      $query->andWhereIn('area_id', $user->getAreaIds());
    }
    
    return $query->innerJoin("$rootAlias.category c")
      ->innerJoin("$rootAlias.area a");
  }
  
  public function getWithCategoryQuery()
  {
    return $this->createQuery('a')
      ->innerJoin('a.category c')
      ->orderBy('a.id desc');
  }
  
  public function getByCategoryQuery($category)
  {
    return $this->getWithCategoryQuery()
      ->addWhere('c.category = ?', $category);
  }
}